//
//  CreateJobSystemDetailVC.swift
//  GreenDeal
//
//  Created by Moennig Q on 2/3/2023.
//

import UIKit
import RxSwift
class CreateJobSystemDetailVC: BaseViewController {
    var editFlag: Bool = false//创建和Review的编辑共用此功能
    let customView = CreateJobSystemDetailView(topBGImageName: "createjob_systemdetails_top_bg")
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.setData()
    }
    //
    private func initUI(){
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.systemDetails())
        self.customView.installationTypeView.additionalTipBtnClosure = {[weak self] in
            guard let strongSelf = self else { return }
            let vc = AddtionalInfomationTipVC()
            vc.hidesBottomBarWhenPushed = true
            strongSelf.navigationController?.pushViewController(vc, animated: false)
        }
    }
    //MARK: - 设置数据
    private func setData(){
        //默认增加两条数据
        if CreateJobModelHelper.shared.createJobViewModel.systemDetail.panels.count == 0 {
            CreateJobModelHelper.shared.createJobViewModel.systemDetail.panels.append(InstallationDetailSystemDetailForPanelModel())
        }
        if CreateJobModelHelper.shared.createJobViewModel.systemDetail.inverters.count == 0 {
            CreateJobModelHelper.shared.createJobViewModel.systemDetail.inverters.append(InstallationDetailSystemDetailForInverterModel())
        }
        self.customView.setData(CreateJobModelHelper.shared.createJobViewModel.systemDetail)
    }
    //MARK: - 获取所有参数
    func getData(){
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.installation_type = self.customView.installationTypeView.viewModel.installation_type
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.additional_capacity_details = self.customView.installationTypeView.viewModel.additional_upgrade_system_details
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.panels = self.customView.combineView.panelTableView.list
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.inverters = self.customView.combineView.inverterTableView.list
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.batteries = self.customView.combineView.batteryCommonView.list
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.is_battery =  self.customView.combineView.batteryCommonView.isBattery.value
        //
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.connected_type = self.customView.gridConnectedView.viewModel.connected_type
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.nmi = self.customView.gridConnectedView.viewModel.nmi
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.gridConnectionApplicationRefNo = self.customView.gridConnectedView.viewModel.gridConnectionApplicationRefNo
        //
        CreateJobModelHelper.shared.createJobViewModel.systemDetail.installed_optimiser = (self.customView.isOptimizereeInstalledSwitchView.switchBtn.tag == 1 ? true: false)
        //调用API计算stcs
        self.getStcsByPanelInfo()
    }
    //MARK: - 根据panel info获取stcs的数量
    private func getStcsByPanelInfo(){
        let service = CreateJobService()
        service.setBlockAfterNetwork { result, otherInfo in
            if let data = result as? Int64{
                CreateJobModelHelper.shared.createJobViewModel.systemDetail.rec_quantity = "\(data)"
                CreateJobModelHelper.shared.systemInfoObserver.onNext(CreateJobModelHelper.shared.createJobViewModel.systemDetail)//通知刷新Review上的STC数量
            }
        } errorBlock: { code, error in
            
        }
        let installDateStr = DateUtil().getDatetimeStr(CreateJobModelHelper.shared.createJobViewModel.installDate, format: "yyyy-MM-dd")
        service.getStcsByPanels(postCode: CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.installAddress.post_code, systemSize: CreateJobModelHelper.shared.createJobViewModel.systemDetail.total_panes_watts, installDate: installDateStr)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
